/*
 * @(#)FeeDetailItemAdapter.java 创建于 2016-07-18 15:59:53
 *
 * 版权：版权所有 Bsoft 保留所有权力。
 */
package hcn.protocol.ods.diagnosis;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * @author <a href="mailto:chinnsii@163.com">zhengshi</a>
 */
public class FeeDetailItemAdapter extends XmlAdapter<FeeDetailItemAdapter.Adapted, AbstractFeeDetailItemBean> {

    /**
     *
     * @param adapted
     * @return
     * @throws Exception
     */
    @Override
    public AbstractFeeDetailItemBean unmarshal(Adapted adapted) throws Exception {
        AbstractFeeDetailItemBean bean;
        if (adapted.position != null) {
            bean = new ExamFeeDetailItemBean();
            ((ExamFeeDetailItemBean) bean).setPosition(adapted.position);
        } else if (adapted.dosage != 0 || adapted.dosageUnit != null || adapted.frequencyCode != null
                || adapted.frequency != null || adapted.usageCode != null || adapted.usage != null || adapted.days != 0) {
            bean = new WesternMedicineFeeDetailItemBean();
            ((WesternMedicineFeeDetailItemBean) bean).setGroupNo(adapted.groupNo);
            ((WesternMedicineFeeDetailItemBean) bean).setDosage(adapted.dosage);
            ((WesternMedicineFeeDetailItemBean) bean).setDosageUnit(adapted.dosageUnit);
            ((WesternMedicineFeeDetailItemBean) bean).setFrequency(adapted.frequency);
            ((WesternMedicineFeeDetailItemBean) bean).setFrequencyCode(adapted.frequencyCode);
            ((WesternMedicineFeeDetailItemBean) bean).setUsage(adapted.usage);
            ((WesternMedicineFeeDetailItemBean) bean).setUsageCode(adapted.usageCode);
            ((WesternMedicineFeeDetailItemBean) bean).setDays(adapted.days);
        } else if (adapted.groupNo != null) {
            bean = new ChineseMedicineFeeDetailItemBean();
            ((ChineseMedicineFeeDetailItemBean) bean).setGroupNo(adapted.groupNo);
        } else {
            bean = new LabFeeDetailItemBean();
        }
        setCommonProperties(bean, adapted);
        return bean;
    }

    /**
     *
     * @param bean
     * @return
     * @throws Exception
     */
    @Override
    public Adapted marshal(AbstractFeeDetailItemBean bean) throws Exception {
        if (bean == null) {
            return null;
        }
        Adapted adapted = new Adapted();
        adapted. itemCode = bean.getItemCode();
        adapted.itemName = bean.getItemName();
        adapted.specification = bean.getSpecification();
        adapted.quantity = bean.getQuantity();
        adapted.price = bean.getPrice();
        adapted.totalFee = bean.getTotalFee();
        adapted.remark = bean.getRemark();

        if (bean instanceof ChineseMedicineFeeDetailItemBean) {
            adapted.groupNo = ((ChineseMedicineFeeDetailItemBean) bean).getGroupNo();
        } else if (bean instanceof ExamFeeDetailItemBean) {
            adapted.position = ((ExamFeeDetailItemBean) bean).getPosition();
        } else if (bean instanceof LabFeeDetailItemBean) {

        } else if (bean instanceof WesternMedicineFeeDetailItemBean) {
            adapted.groupNo = ((WesternMedicineFeeDetailItemBean) bean).getGroupNo();
            adapted.dosage = ((WesternMedicineFeeDetailItemBean) bean).getDosage();
            adapted.dosageUnit = ((WesternMedicineFeeDetailItemBean) bean).getDosageUnit();
            adapted.frequencyCode = ((WesternMedicineFeeDetailItemBean) bean).getFrequencyCode();
            adapted.frequency = ((WesternMedicineFeeDetailItemBean) bean).getFrequency();
            adapted.usageCode = ((WesternMedicineFeeDetailItemBean) bean).getUsageCode();
            adapted.usage = ((WesternMedicineFeeDetailItemBean) bean).getUsage();
            adapted.days = ((WesternMedicineFeeDetailItemBean) bean).getDays();
        }

        return adapted;
    }

    /**
     *
     * @param bean
     * @param adapted
     */
    private void setCommonProperties(AbstractFeeDetailItemBean bean, Adapted adapted) {
        bean.setItemCode(adapted.itemCode);
        bean.setItemName(adapted.itemName);
        bean.setTotalFee(adapted.totalFee);
        bean.setSpecification(adapted.specification);
        bean.setQuantity(adapted.quantity);
        bean.setPrice(adapted.price);
        bean.setRemark(adapted.remark);
    }

    @XmlType(namespace = "feeDetailItem")
    public static class Adapted {

        @XmlElement
        private String groupNo;
        @XmlElement
        private String itemCode;
        @XmlElement
        private String itemName;
        @XmlElement
        private String specification;
        @XmlElement
        private double quantity;
        @XmlElement
        private String price;
        @XmlElement
        private String totalFee;
        @XmlElement
        private String remark;
        @XmlElement
        private String position;
        @XmlElement
        private double dosage;
        @XmlElement
        private String dosageUnit;
        @XmlElement
        private String frequencyCode;
        @XmlElement
        private String frequency;
        @XmlElement
        private String usageCode;
        @XmlElement
        private String usage;
        @XmlElement
        private int days;

    }
}
